# Maintainer: Milan P. Stanić <mps@arvanta.net>

_flavor=gru
pkgname=linux-${_flavor}
# NOTE: this kernel is intended for testing
# please resist urge to upgrade it blindly
pkgver=6.8.9
case $pkgver in
	*.*.*)	_kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=0
pkgdesc="Linux latest stable kernel for $_flavor chromebook"
url="https://www.kernel.org"
_depends_dev="perl gmp-dev flex bison"
makedepends="$_depends_dev sed installkernel bc linux-headers linux-firmware-any
	openssl-dev diffutils findutils vboot-utils u-boot-tools dtc"
options="!strip !check"
install=

#subpackages="$pkgname-dev:_dev:$CBUILD_ARCH"
source="https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$_kernver.tar.xz"
case $pkgver in
	*.*.0)	source="$source";;
	*.*.*)	source="$source
	https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/patch-$pkgver.xz" ;;
esac

	source="$source
	config
	kernel.its.$_flavor
	fix-rk3399-gru-mmc-order.patch
	fix-gru-analogix_dp_core.patch
	PATCH-v4-00-11-media-rkvdec-Add-H.264-High-10-and-4-2-2-profile-support.patch
	"

arch="aarch64"
_carch="arm64"
license="GPL-2.0"

prepare() {
	local _patch_failed=
	cd "$srcdir"/linux-$_kernver
	case $pkgver in
		*.*.0);;
		*)
		msg "Applying patch-$pkgver.xz"
		unxz -c < "$srcdir"/patch-$pkgver.xz | patch -p1 -N ;;
	esac

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# remove localversion from patch if any
	rm -f localversion*
	oldconfig
}

oldconfig() {
		local _builddir="$srcdir"/linux-$_kernver
		echo "-$pkgrel-$_flavor" > "$_builddir"/localversion-alpine \
			|| return 1

		cp "$srcdir"/config "$_builddir"/.config
		make -C "$srcdir"/linux-$_kernver \
			O="$_builddir" \
			ARCH="$_carch" \
			listnewconfig oldconfig
}

build() {
	unset LDFLAGS
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"
	cd "$srcdir"/linux-$_kernver
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
}

_package() {
	local _buildflavor="$1" _outdir="$2"
	local _abi_release=${pkgver}-${pkgrel}-${_buildflavor}
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

	cd "$srcdir"/linux-$_kernver
	# modules_install seems to regenerate a defect Modules.symvers on s390x. Work
	# around it by backing it up and restore it after modules_install
	cp Module.symvers Module.symvers.backup

	mkdir -p "$_outdir"/boot "$_outdir"/lib/modules

	local _install
	case "$CARCH" in
		arm*|aarch64) _install="zinstall dtbs_install";;
		*) _install=install;;
	esac

	make -j1 modules_install $_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$_outdir" \
		INSTALL_MOD_STRIP=1 \
		INSTALL_PATH="$_outdir"/boot \
		INSTALL_DTBS_PATH="$_outdir/boot/dtbs-$_buildflavor"

	cp Module.symvers.backup Module.symvers

	rm -f "$_outdir"/lib/modules/${_abi_release}/build \
		"$_outdir"/lib/modules/${_abi_release}/source
	rm -rf "$_outdir"/lib/firmware

	install -D -m644 include/config/kernel.release \
		"$_outdir"/usr/share/kernel/$_buildflavor/kernel.release
}

# main flavor installs in $pkgdir
package() {

	_package $_flavor "$pkgdir"
	local _builddir="$srcdir"/linux-$_kernver
	#cp $_builddir/arch/arm64/boot/Image.gz $pkgdir/boot/vmlinuz-$_flavor
	cd $_builddir
	cp $srcdir/kernel.its.$_flavor .
	mkimage -D "-I dts -O dtb -p 2048" -f kernel.its.$_flavor vmlinux.uimg

	dd if=/dev/zero of=bootloader.bin bs=512 count=1
	echo "console=tty0 earlyprintk=tty0 init=/sbin/init root=PARTUUID=%U/PARTNROFF=1 rootwait rw mitigations=off loglevel=7" > cmdline
	vbutil_kernel \
		--pack $pkgdir/boot/vmlinux.kpart-$pkgver-$pkgrel-$_flavor \
		--version 1 \
		--vmlinuz vmlinux.uimg \
		--arch aarch64 \
		--keyblock /usr/share/vboot/devkeys/kernel.keyblock \
		--signprivate kernel_data_key.vbprivk \
		--signprivate /usr/share/vboot/devkeys/kernel_data_key.vbprivk \
		--config cmdline \
		--bootloader bootloader.bin

	rm cmdline bootloader.bin
	#rm $pkgdir/boot/vmlinuz-$_flavor
	rm $pkgdir/boot/System.map-$pkgver-$pkgrel-$_flavor
	cd $pkgdir/boot
	ln -s vmlinux.kpart-$pkgver-$pkgrel-$_flavor vmlinux.kpart 
}

_dev() {
	local _flavor=$(echo $subpkgname | sed -E 's/(^linux-|-dev$)//g')
	local _abi_release=${pkgver}-${pkgrel}-$_flavor
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for $_flavor kernel"
	depends="$_depends_dev"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$srcdir"/config-$_flavor.${CARCH} "$dir"/.config
	echo "-$pkgrel-$_flavor" > "$dir"/localversion-alpine

	make -j1 -C "$srcdir"/linux-$_kernver O="$dir" ARCH="$_carch" \
		syncconfig prepare modules_prepare scripts

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the soruces
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk
	cd "$srcdir"/linux-$_kernver
	find .  -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' -o -name 'Platform' \) \
		-print | cpio -pdm "$dir"

	cp -a scripts include "$dir"

	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$srcdir"/build-$_flavor.$CARCH/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}

sha512sums="
5c4eb4aa1d3f7d1ea01c0f7ddeadacdece6e144fd4bdfc16b2b925d3e10dc04de3a6db69320b79a96c3560052616f001d2c09e7a1bb4f7b731e2380a7ecce068  linux-6.8.tar.xz
89c036fac161c5df8c594d62ccc7c841ae2ea19b9e0c66cdb2a4404101e39bef765c0ccc774bd81a281ab160cdff83f3a64beac7b616eb337cdef908de10ad65  patch-6.8.9.xz
54e51ba6cef0c553a9f01af060882c4fa346743b5bef9c922cd740047e9f76d043a9756af080d8d7c0da0996abd032815bad1861b45e2f74b6bccf632b92c352  config
b5a2fbc0560201b4ec1d21e890cc5290c58d0544105a5bfabfe04e76bdf54b64dc07076c40414cedb38b12687cf1ae3f773ecfcac02e3d1edd84f6ef80c951da  kernel.its.gru
1653cd164857fb433c32203df8254d27c28bcf64e4bd5d0291ce95a8d7e5b48815ea6e81e32060b9a4c0cb13c302f9accc8e77451e30138669337f0d20910a76  fix-rk3399-gru-mmc-order.patch
476188e5a6967fe061293a6d5b540158869f7f09eba3b0eb651bb7948003c050aa7e419c90f3135316f2ce8c1bb4a411bd3c82cde85dddbf17d971c5198893fe  fix-gru-analogix_dp_core.patch
4b1a494a3821d1829a2a2bb6f959ecbfc1a2e750e8bcf5154701d13478a87e01468848182c8bad993aedf55d5e43c69d360f90a8023f90018b099a7a0dacb7aa  PATCH-v4-00-11-media-rkvdec-Add-H.264-High-10-and-4-2-2-profile-support.patch
"
